package com.SparkCore.RDD.Operator.Transform

import org.apache.spark.{SparkConf, SparkContext}


/**
 * 想获取每个数据所在的分区
 */
object Spark03_RDD_Operator_Transform1 {

  def main(args: Array[String]): Unit = {

    //创建环境
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("RDD")
    val sparkContext = new SparkContext(sparkConf)

    //创建RDD算子 -- map
    val rdd = sparkContext.makeRDD(List(1,2,3,4))

    val mpiRDD = rdd.mapPartitionsWithIndex(
      (index , iter ) => {
          iter.map(
            num =>
              (index , num)
          )
      }
    )
    mpiRDD.collect().foreach(println )

    //关闭环境
    sparkContext.stop()
  }
}
